
import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author Andrea
 */
public class XMLHandler {
        final static double HOME_LAT = 28.01;       //VERIFY WITH JON   
        final static double HOME_LONG = -26.17;
        final static double BLOCK_LENGTH = 0.000889;

    public static XMLDTO extract_xml(String xml_path) { // return type must change to XMLDTO
         
        
        int code=-1;
        String user="Default";
        String team="Default";
        Timestamp timestamp = new Timestamp(0);
        int row = -1;
        int column = -1;
        int queryID = -1;
        
        try {
            File xml = new File(xml_path);
            DocumentBuilderFactory dbfact = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbfact.newDocumentBuilder();
            Document xml_doc = db.parse(xml);
            xml_doc.getDocumentElement().normalize();
            NodeList node = xml_doc.getElementsByTagName(xml_doc.getDocumentElement().getNodeName()); //gets root tag

            for (int i = 0; i <= node.getLength() - 1; i++) {
                Node first_node = node.item(i);

                if (first_node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) first_node;
                    
                    code = Integer.parseInt(getElementData(element, "code"));
                    user = getElementData(element, "user");
                    team = getElementData(element, "team");
                    timestamp.setTime(Long.parseLong(getElementData(element, "timestamp")));
                    row = latToRow (Double.parseDouble(getElementData(element, "latitude")));
                    column = longToColumn (Double.parseDouble(getElementData(element, "longitude")));
                    queryID = Integer.parseInt(getElementData(element, "queryID"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        XMLDTO xml_obj = new XMLDTO(code, user,  team,  row,  column,  queryID, timestamp);
        return xml_obj;

    }

    public static String getElementData(Element el, String tag) 
    {
        String data = "";

        NodeList node_list = el.getElementsByTagName(tag);

        if (node_list.getLength() != 0) {
            Element current_element = (Element) node_list.item(0);
            NodeList node = current_element.getChildNodes();
            data = node.item(0).getNodeValue();
        }

        return data;
    }
    
    private static int latToRow (double lat)
    {
        return (int) Math.round(Math.abs(HOME_LAT - lat)/BLOCK_LENGTH);
    }
    
    private static int longToColumn (double longitude)
    {
        return (int) Math.round(Math.abs(longitude - HOME_LONG)/BLOCK_LENGTH);
    }
}